package com.jn.viewtest_2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.TextView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView implements View.OnClickListener {
    public MyTextView(Context context) {
        super(context);
        initView();
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        setOnClickListener(this);
        if (TextUtils.isEmpty(getText())) {
            setText("123");
        }
        setGravity(Gravity.CENTER);
    }

    @Override
    public void onClick(View v) {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5F,
                Animation.RELATIVE_TO_SELF, 0.5F);
        rotateAnimation.setDuration(500);
        startAnimation(rotateAnimation);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(10);

        int baseline = getBaseline();
        int width = getWidth();
        int height = getHeight();

        //canvas.drawLine(0, baseline, width, baseline, paint);

        /*canvas.drawLines(new float[] {
                0, 0, width/2, height/2,
                width/2, height/2, width, 0,
                width, 0, width, height,
                width, height, 0, height,
                0, height, 0, 0
        }, paint);*/

        Rect rect = new Rect(0, 0, width, height);
        canvas.drawRect(rect, paint);

        canvas.drawCircle(width/2, height/2, height/2, paint);

        paint.setTextSize(90);
        canvas.drawText("Hello", 30, baseline, paint);
    }
}